<?php
/***********
常用函数模块
lsj 2010-03-29
***********/

if(!defined('APP_HOME')) exit('Access Denied');

/**
 * 新建多层目录，一层层地建立各目录
 */
function makeDir($dir)
{
	if(!$dir) return false;
	$ds = '/';
	$dir = str_replace("\\", $ds, $dir);
	$dirs  = explode($ds, trim($dir, $ds));
	$nd = '';
	foreach($dirs as $d){
		if(is_dir($nd)) continue;
        if(strpos($d, ':')){
			$nd .= $d;
			continue;
		}
			$nd .= $ds . $d;
		if(!mkdir($nd, 0700)){
			break;
			throw new Exception('The mkdir "'.$nd.'" is failed.');
		}
	}
	return true;
}

/**
 * 获取客户端IP
 */
function getIp()
{
	if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && !empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
		$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}elseif(isset($_SERVER["HTTP_CLIENT_IP"]) && !empty($_SERVER["HTTP_CLIENT_IP"])){
		$realip = $_SERVER["HTTP_CLIENT_IP"];
	}else{
		$realip = $_SERVER["REMOTE_ADDR"];
	}
	return $realip;
}

/**
 * 获取随机字符串
 * $length　产生的字符串的长度
 * $type　num:纯数字 string:纯字母 all:数字加字母
 */
function getRandNum($length = 4, $type = 'num')
{
	$text = '';
	$mb = array();
	for($i = 0; $i < $length; $i++){
		$nn = mt_rand() % 3;
		if($type == 'all'){
			if($nn == 0) $mb = array(65, 90);
			elseif ($nn == 1) $mb = array(97, 122);
			else $mb = array(48, 57);
		}elseif($type == 'string'){
			if($nn == 0) $mb = array(65, 90);
			else $mb = array(97, 122);
		}else{
			$mb = array(48, 57);
		}

		$text .= chr(mt_rand($mb[0], $mb[1]));
	}
	return $text;
}

/**
 * 简单输出一张验证码的图片来
 * $text 图片的内容，一般为字符
 * $mod 显示的样式，有三种
 */
function creatImg($text, $mod = 3)
{
	if(headers_sent()){
		echo 'ERROR:HTTP标头已被发出';
		return false;
	}
	$height = 20;
	$len = strlen($text);
	$width = $len * 12;
	$img = imageCreate($width, $height);
	$white = ImageColorAllocate($img, 255, 255, 255);
	$black = ImageColorAllocate($img, 0, 0, 0);
	ImageRectangle($img, 0, 0, $width - 1, $height - 1, $black);
	if($mod == 3){
		for ($i = 1; $i <= 100; $i++) {
			imageString($img, 1, mt_rand(1, $width - 5), mt_rand(1, $height - 7), '*', imageColorAllocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)));
		}
		for ($i = 0; $i < $len; $i++){
			imageString($img, mt_rand(4, 10), $i * 10 + mt_rand(1, 6), mt_rand(1, $height / 4), $text[$i], imageColorAllocate($img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200)));
		}
	} elseif ($mod == 2){
		for($i = 0; $i < 90; $i++){
			imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$black);
			imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img, 255, 123, 005));
		}
		imagestring($img, 10, 6, 2, $text, $black);
	}else{
		ImageRectangle($img, 0, 0, $width - 1, $height - 1, $white);
		imagestring($img, 10, 6, 2, $text, $black);
	}

	Header("Content-type: image/png");
	ImagePng($img);
	ImageDestroy($img);
}

/**
 * 设置本站的COOKIE
 */
function cookie($ckname, $ckvalue, $cktime = 0, $path = '/')
{
	if(!headers_sent()){
		if($cktime != 0) $cktime = $_SERVER['REQUEST_TIME'] + $cktime;
		return setcookie($ckname, $ckvalue, $cktime, $path);
	}
	return false;
}

/**
 * 过滤HTML标签
 */
function filterHtml($str)
{
    $str=stripslashes($str);

    $str=preg_replace("/\s+/", ' ', $str); //过滤多余回车
    $str=preg_replace("/<[ ]+/si",'<',$str); //过滤<__("<"号后面带空格)

    $str=preg_replace("/<\!--.*?-->/si",'',$str); //注释
    $str=preg_replace("/<(\!.*?)>/si",'',$str); //过滤DOCTYPE
    $str=preg_replace("/<(\/?html.*?)>/si",'',$str); //过滤html标签
    $str=preg_replace("/<(\/?head.*?)>/si",'',$str); //过滤head标签
    $str=preg_replace("/<(\/?meta.*?)>/si",'',$str); //过滤meta标签
    $str=preg_replace("/<(\/?body.*?)>/si",'',$str); //过滤body标签
    $str=preg_replace("/<(\/?link.*?)>/si",'',$str); //过滤link标签
    $str=preg_replace("/<(\/?form.*?)>/si",'',$str); //过滤form标签
    $str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签

    $str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si",'',$str); //过滤applet标签
    $str=preg_replace("/<(\/?applet.*?)>/si",'',$str); //过滤applet标签

    $str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si",'',$str); //过滤style标签
    $str=preg_replace("/<(\/?style.*?)>/si",'',$str); //过滤style标签

    $str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si",'',$str); //过滤title标签
    $str=preg_replace("/<(\/?title.*?)>/si",'',$str); //过滤title标签

    $str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si",'',$str); //过滤object标签
    $str=preg_replace("/<(\/?objec.*?)>/si",'',$str); //过滤object标签

    $str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si",'',$str); //过滤noframes标签
    $str=preg_replace("/<(\/?noframes.*?)>/si",'',$str); //过滤noframes标签

    $str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si",'',$str); //过滤frame标签
    $str=preg_replace("/<(\/?i?frame.*?)>/si",'',$str); //过滤frame标签

    $str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si",'',$str); //过滤script标签
    $str=preg_replace("/<(\/?script.*?)>/si",'',$str); //过滤script标签
    $str=preg_replace("/javascript/si","JAVASCRIPT",$str); //过滤script标签
    $str=preg_replace("/vbscript/si","VBSCRIPT",$str); //过滤script标签
    $str=preg_replace("/on([a-z]+)\s*=/si","ON\\1=",$str); //过滤script标签
    $str=preg_replace("/&#/si","&＃",$str); //过滤script标签，如javAsCript:alert('aabb)

    $str=addslashes($str);
    return($str);
}


/**
 * 发送邮件
 * $subject: 标题 
 * $content: 内容
 * $addr: 地址，可以是数组。群发
 * $mode: 群发的模式，1：循环单发 2：抄送形式 3：密送形式
 */
function sendMail($subject, $content, $addr, $mode = 1)
{
    $info = loadConfig('mail');
    $mailer = new lib_class_mail;

    return ture;

    
	
	
}








